<template>
  <div class="BookBorrowing">
    <el-button type="primary" @click="goToScanPage2">扫码借书</el-button>
    <!-- 图书推荐区域 -->
    <el-card shadow="hover">
      <div style="font-size: 14px; font-weight: bold; margin-bottom: 10px;">
        推荐图书
      </div>
      <el-row :gutter="20">
        <el-col :span="12" v-for="book in recommendedBooks" :key="book.id">
          <el-card shadow="hover" >
            <div  style="font-size: 9px;padding: 5px;padding-right: 0px;padding-left: 0px;margin-bottom: 0px;">
              <p style="padding-left: 0px;margin-bottom: 0px;"><strong>书名：</strong>{{ book.bookname }}</p>
              <p style="padding-left: 0px;margin-bottom: 0px;"><strong>作者：</strong>{{ book.author }}</p>
              <p style="padding-left: 0px;margin-bottom: 0px;"><strong>分类：</strong>{{ book.category }}</p>
              <p style="padding-left: 0px;margin-bottom: 0px;"> <strong>借阅次数：</strong>{{ book.borrow_count }}</p>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-card>

    <!-- 图书列表 -->
    <el-card style="margin-top: 0px; font-size:10px">
      <el-table :data="bookList" border style="width: 100%;font-size:10px">
        <el-table-column prop="id" label="ISBN" width="60"> </el-table-column>
        <el-table-column prop="bookname" label="书名" width="70">
        </el-table-column>
        <el-table-column prop="author" label="作者" width="67">
        </el-table-column>
        <el-table-column prop="inventory" label="库存" width="55">
        </el-table-column>
        <el-table-column label="操作" width="70">
          <!-- 凹槽 -->
          <template slot-scope="scope">
            <el-button
              type="success"
              icon="el-icon-shopping-cart-2"
              @click="open(scope.row)"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block" style="margin-top: 20px;">
        <!-- <span class="demonstration">完整功能</span> -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[2, 3, 5, 10]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <!-- page-size每页显示条目个数 -->
      </div>

      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="60%"
        :before-close="handleClose"
      >
        <span>是否确定借阅？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="JoinBorrowing"style="position: relative;left: -5px;">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { mapState } from "vuex";
import gettime from "../utlis/gettime";
import { SelectAllBookAPI, JoinBorrowingAPI,recommendBooksAPI } from "@/request/api";
export default {
  computed: {
    //解构
    ...mapState(["username"]),
  },
  data() {
    return {
      //当前页数
      currentPage: 1,
      size: 5,
      bookList: [],
      total: 1,
      dialogVisible: false,
      BorrowingBook: {
        bookname: "",
        time: "",
        username: "",
      },
      recommendedBooks: [], // 推荐的图书数据
    };
  },

  methods: {
    goToScanPage2(){
      this.$router.push({ path: "/user/BookScanBorrowing" }); // 跳转到扫码页面
    },
    //打开Dialog
    open(row) {
      this.BorrowingBook.bookname = row.bookname;
      this.BorrowingBook.username = this.username;
      // 使用 gettime() 获取当前时间字符串并进行赋值
      this.BorrowingBook.time = gettime();
      this.dialogVisible = true;

      // console.log(this.BorrowingBook.bookname);
      // console.log(this.BorrowingBook.username);
      // console.log(this.BorrowingBook.time);
    },
    //确定借阅
    async JoinBorrowing() {
      let res = await JoinBorrowingAPI(this.BorrowingBook);
      // console.log(res);
      if (res.status == 0) {
        //借阅成功
        this.$message({
          message: res.message,
          type: "success",
        });
        this.dialogVisible = false;
        this.getAllBooks();
      } else {
        this.$message.error(res.message);
      }
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleSizeChange(val) {
      this.size = val;
      this.getAllBooks();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getAllBooks();
    },
    //查询所有图书
    async getAllBooks() {
      let obj = {
        currentPage: this.currentPage,
        size: this.size,
      };
      let res = await SelectAllBookAPI(obj);
      this.bookList = res.data.result;
      this.total = res.data.total >= 1 ? res.data.total : 5;
      console.log(res);
    },
        // 查询推荐图书
    async getRecommendedBooks() {
      let res = await recommendBooksAPI({
        currentPage: 1,
        size: 4, // 限制返回的推荐书数量
      });
      if (res.status === 0) {
        this.recommendedBooks = res.data;
      } else {
        this.$message.error(res.message);
      }
    },
  },
  created() {
    this.getAllBooks();
    this.getRecommendedBooks(); // 加载推荐图书
  },
};
</script>

<style>

.el-card__body, .el-main {
  padding: 10px !important;
  }
  .el-row {
    margin-top: 0px !important;
}
element.style {
    font-size: 14px;
    font-weight: bold;
    margin-bottom: 0px !important;
}
.el-page-header {
    display: flex;
    line-height: 0px!important;
}
.el-page-header__title {
    font-size: 14px;
    font-weight: 500;
    position: absolute;
    top: 7px;
    left: 200px;
}
.el-button--primary {
    color: #FFF;
    background-color: #409EFF;
    border-color: #409EFF;
    position: relative;
    left: 250px;
}
.el-card {
    border-radius: 4px;
    border: 1px solid #EBEEF5;
    background-color: #FFF;
    overflow: hidden;
    color: #303133;
    transition: .3s;
    position: relative;
}
.el-pagination {
    white-space:normal !important;
    padding: 2px 5px;
    color: #303133;
    font-weight: 700;
}
</style>